﻿using System;
using System.Runtime.Serialization;

namespace RockBus.DataContracts
{
    [DataContract(Namespace = StringConstants.RockBusNamespace)]
    public class SubscriptionDetail : Detail
    {
        private SubscriptionDetail()
            : base()
        {
        }

        public SubscriptionDetail(string parentId, string id, string name, string endpointId, bool persistent, bool paused = false, DateTime? expiryDateTimeUtc = null, string requestPipelineId = null, string responsePipelineId = null)
            : base(parentId, id, name, endpointId, persistent, paused, expiryDateTimeUtc, requestPipelineId, responsePipelineId)
        {
            if (expiryDateTimeUtc.HasValue && (expiryDateTimeUtc.Value != DateTime.MinValue))
            {
                this.ExpiryDateTimeUtc = expiryDateTimeUtc;
            }
        }

        public SubscriptionDetail(string parentId, string id, string name, string endpointId, bool persistent, long? messageValidityDurationSecs, long? deliveryAttemptIntervalSecs, int? maxDeliveryAttemptCount, bool paused = false, DateTime? expiryDateTimeUtc = null, string requestPipelineId = null, string responsePipelineId = null)
            : this(parentId, id, name, endpointId, persistent, paused, expiryDateTimeUtc, requestPipelineId, responsePipelineId)
        {
            if (messageValidityDurationSecs.HasValue && (messageValidityDurationSecs > 0))
            {
                this.MessageValidityDurationSecs = messageValidityDurationSecs;
            }
            if (deliveryAttemptIntervalSecs.HasValue && (deliveryAttemptIntervalSecs > 0))
            {
                this.DeliveryAttemptIntervalSecs = deliveryAttemptIntervalSecs;
            }
            if (maxDeliveryAttemptCount.HasValue && (maxDeliveryAttemptCount > 0))
            {
                this.MaxDeliveryAttemptCount = maxDeliveryAttemptCount;
            }
        }

        public SubscriptionDetail(string parentId, string id, string name, string endpointId, bool persistent, TimeSpan? messageValidityDuration, TimeSpan? deliveryAttemptInterval, int? maxDeliveryAttemptCount, bool paused = false, DateTime? expiryDateTimeUtc = null, string requestPipelineId = null, string responsePipelineId = null)
            : this(parentId, id, name, endpointId, persistent, paused, expiryDateTimeUtc, requestPipelineId, responsePipelineId)
        {
            if (messageValidityDuration.HasValue && (messageValidityDuration.Value != TimeSpan.Zero))
            {
                this.MessageValidityDurationSecs = Convert.ToInt64(messageValidityDuration.Value.TotalSeconds);
            }
            if (deliveryAttemptInterval.HasValue && (deliveryAttemptInterval.Value != TimeSpan.Zero))
            {
                this.DeliveryAttemptIntervalSecs = Convert.ToInt64(deliveryAttemptInterval.Value.TotalSeconds);
            }
            if (maxDeliveryAttemptCount.HasValue && (maxDeliveryAttemptCount > 0))
            {
                this.MaxDeliveryAttemptCount = maxDeliveryAttemptCount;
            }
        }

        [DataMember]
        public Nullable<long> MessageValidityDurationSecs { get; set; }

        [DataMember]
        public Nullable<long> DeliveryAttemptIntervalSecs { get; set; }

        [DataMember]
        public Nullable<int> MaxDeliveryAttemptCount { get; set; }
    }
}